/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.matezk.dms.things.model.entity;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.matezk.dms.common.config.mybatis.JacksonTypeHandler;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import top.continew.starter.extension.crud.model.entity.BaseDO;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备实体
 *
 * @author 程道林
 * @since 2024/12/02 10:59
 */
@Data
@TableName(value = "t_device", autoResultMap = true)
public class DeviceDO extends BaseDO {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    protected Long id;

    /**
     * 设备ID
     */
    protected String deviceId;

    /**
     * 产品ID
     */
    protected String productId;

    /**
     * 设备名称
     */
    protected String deviceName;

    /**
     * 设备密钥
     */
    private String deviceSecret;

    /**
     * 家庭ID
     */
    protected String familyId;

    /**
     * 部门ID
     */
    protected Long deptId;

    /**
     * 别名
     */
    protected String aliasName;

    /**
     * 物联网卡号
     */
    protected String iccid;

    /**
     * 状态
     */
    protected Integer deviceStatus;

    /**
     * 信号强度
     */
    protected Integer csq;

    /**
     * 期望属性
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    protected JSONObject desired;

    /**
     * 上报属性
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    protected JSONObject reported;

    /**
     * 通知配置
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    protected NotifyConfig notifyConfig;

    /**
     * 打印时间
     * 
     * @return
     */
    protected Date printTime;

    public Fid getDesiredFid() {
        return ObjectUtil.isNotNull(desired) && desired.containsKey("fid") ? desired.getBean("fid", Fid.class) : null;
    }

    public Fid getReportedFid() {
        return ObjectUtil.isNotNull(reported) && reported.containsKey("fid")
            ? reported.getBean("fid", Fid.class)
            : null;
    }

    public Guard getDesiredGuard() {
        return ObjectUtil.isNotNull(desired) && desired.containsKey("guard")
            ? desired.getBean("guard", Guard.class)
            : null;
    }

    public Guard getReportedGuard() {
        return ObjectUtil.isNotNull(reported) && reported.containsKey("guard")
            ? reported.getBean("guard", Guard.class)
            : null;
    }

    public Standby getDesiredStandby() {
        return ObjectUtil.isNotNull(desired) && desired.containsKey("standby")
            ? desired.getBean("standby", Standby.class)
            : null;
    }

    public Standby getReportedStandby() {
        return ObjectUtil.isNotNull(reported) && reported.containsKey("standby")
            ? reported.getBean("standby", Standby.class)
            : null;
    }

    public Integer getDesiredMode() {
        return ObjectUtil.isNotNull(desired) && desired.containsKey("mode") ? desired.getInt("mode") : null;
    }

    public Integer getReportedMode() {
        return ObjectUtil.isNotNull(reported) && reported.containsKey("mode") ? reported.getInt("mode") : null;
    }

    public String getProductId() {
        return ObjectUtil.isEmpty(deviceId) ? "" : deviceId.split("/")[0];
    }

    public String getDeviceName() {
        return ObjectUtil.isEmpty(deviceId) ? "" : deviceId.split("/")[1];
    }

    public NotifyConfig getNotifyConfig() {
        if ((ObjectUtil.isNull(notifyConfig) || notifyConfig.isEmpty()) && ObjectUtil.equals("YXENYPLCQH", productId)) {
            notifyConfig = new NotifyConfig();
            List<NotifyConfig.Event> events = new ArrayList<>();
            NotifyConfig.Event eventOn = new NotifyConfig.Event();
            eventOn.setEventId("power_on");
            eventOn.setWechatEnable(Boolean.FALSE);
            eventOn.setSmsEnable(Boolean.FALSE);
            eventOn.setVoiceEnable(Boolean.FALSE);
            events.add(eventOn);
            NotifyConfig.Event eventOff = new NotifyConfig.Event();
            eventOff.setEventId("power_off");
            eventOff.setWechatEnable(Boolean.FALSE);
            eventOff.setSmsEnable(Boolean.FALSE);
            eventOff.setVoiceEnable(Boolean.FALSE);
            events.add(eventOff);
            notifyConfig.setEvents(events);
            notifyConfig.setContacts(new ArrayList<>());
        }
        return notifyConfig;
    }
}